/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models.dados;

import config.LogErro;
import java.io.Serializable;
import java.util.List;
import javax.swing.JOptionPane;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;

/**
 *
 * @author Wenderson
 */
public class HibernateDAO<T, Type extends Serializable> implements HibernateGeneric<T, Type> {

    //private static final Log logger = LogFactory.getLog(HibernateDAO.class);
    private Class<T> persistentClass;
    
    public HibernateDAO(Class persistent) {
        super();
        
        this.persistentClass = persistent;
    }
    
    @Override
    public void beginTransaction() {
        HibernateUtil.beginTransaction();
    }

    @Override
    public void commitTransaction() {
        HibernateUtil.commitTransaction();
    }

    @Override
    public void saveOrUpdate(T entity) {
        try {
            this.beginTransaction();
            
            HibernateUtil.getSession().saveOrUpdate(entity);
            
            this.commitTransaction();
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
            //HibernateDAO.logger.error(e);
            HibernateUtil.rollBackTransaction();
        } finally {
            HibernateUtil.closeSession();
        }
    }
    
    @Override
    public T obter(Type id) {
        try {
            //this.beginTransaction();
            
            T entity = (T) HibernateUtil.getSession().load(this.persistentClass, id);
            
            return entity;
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
        } finally {
            //HibernateUtil.closeSession();
        }
        return null;
    }

    @Override
    public void delete(T entity) {
        try {
            this.beginTransaction();
            
            HibernateUtil.getSession().delete(entity);
            
            this.commitTransaction();
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
            //HibernateDAO.logger.error(e);
            HibernateUtil.rollBackTransaction();
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void deleteId(Type id) {
        try {
            this.beginTransaction();
            
            T entity = (T) HibernateUtil.getSession().load(this.persistentClass, id);
            
            HibernateUtil.getSession().delete(entity);
            
            this.commitTransaction();
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
            HibernateUtil.rollBackTransaction();
        } finally {
            HibernateUtil.closeSession();
        }
    }

    @Override
    public List<T> listAll() {
        try {
            //this.beginTransaction();
            
            Criteria criteria = HibernateUtil.getSession().createCriteria(this.persistentClass);
            
            return criteria.list();
            
        } catch (Exception e) {
            LogErro.gravaErro(e);e.printStackTrace();
            //HibernateDAO.logger.error(e);
            //javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
        } finally {
            
        }
        
        return null;
    }
    
}
